//
//  JHSpecialViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHSpecialViewController.h"
#import "JHSpcialTableViewCell.h"
#import "JHSpecialXQViewController.h"
#import "NetwordHandler.h"
#import "MJRefresh.h"

@interface JHSpecialViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView *specialTable;
@property(nonatomic,retain)NSMutableArray *spArr;
//@property(nonatomic,retain)NSMutableDictionary *Dic;
@end

@implementation JHSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.nextCursor = 1;
    self.spArr = [NSMutableArray array];
    [self createSpecial];
    [self addHeader];
    [self addFooter];
}

- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHSpecialViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.specialTable addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:1];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.specialTable headerBeginRefreshing];
    
}
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block JHSpecialViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.specialTable addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:(vc.nextCursor++)+1];//请求更多数据
        
    }];
}
- (void)GetPersonList:(NSInteger)nextCursor
{
    if (nextCursor <= 10) {
        
   
    /**
     *  获取数据
     *
     *  @param result 获取数组
     *
     *  @return 调用
     */
        NSString *s = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_list?page=%ld&per_page=10",nextCursor];
    [NetwordHandler requestWithURL:s completion:^(id result) {
//        self.Dic = [NSMutableDictionary dictionary];
        NSMutableDictionary *Dic = result;
//        self.title = [Dic objectForKey:@"moduleTitle"];
        NSMutableArray *arr = [Dic objectForKey:@"list"];


        for (NSDictionary *list in arr) {

            JHSpecial *sp = [[JHSpecial alloc] initWithDic:list];
            [_spArr addObject:sp];
        }
//        NSLog(@"*********%@",_spArr);

        [self.specialTable headerEndRefreshing];
        [self.specialTable footerEndRefreshing];
        
        [self.specialTable reloadData];

    }];}
}

- (void)createSpecial
{
    //  创建专题TableView
    self.specialTable = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 90) style:UITableViewStylePlain] autorelease];
    _specialTable.separatorColor = [UIColor clearColor];
    _specialTable.delegate = self;
    _specialTable.dataSource = self;
    [self.view addSubview:_specialTable];
    [_specialTable registerClass:[JHSpcialTableViewCell class] forCellReuseIdentifier:@"s"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //重用池
    JHSpcialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"s"];
    cell.special = _spArr[indexPath.section];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //  行数
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //  分区数
    return _spArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat f = self.view.frame.size.height / 3;
    return f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  点击方法
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    JHSpecialXQViewController *XQ = [[JHSpecialXQViewController alloc]init];
    XQ.str = [_spArr[indexPath.section] specialId];
    [self.navigationController pushViewController:XQ animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
